package cn.djun.news.controller;

import cn.djun.news.entity.User;
import cn.djun.news.service.UserService;
import cn.djun.news.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author djun
 * @classname UserController
 * @description http://localhost:8088/swagger-ui.html#/
 * @date 2020/11/24 10:43
 * @email 3170495286@qq.com
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户管理API")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private TokenUtil tokenUtil;

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "查询用户信息", notes = "根据id查询用户", httpMethod = "GET")
    public ResponseEntity<BaseResponse<User>> getUserInfo(@PathVariable("id") int id) {
        try {
            User user = userService.getUserById(id);
            if (user == null) {
                return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.USRE_ERROR, "");
            }
            return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.SELECT_SUCCES, user);
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }

    /**
     * 新增用户
     *
     * @param request
     * @param user
     * @return
     */
    @PutMapping("/register")
    @ApiOperation(value = "新增用户", notes = "注册用户", httpMethod = "PUT")
    public ResponseEntity<BaseResponse> addUser(HttpServletRequest request, @RequestBody User user) {
        try {
            //判断用户名是否重复
            if (userService.isUserName(user.getUserName())) {
                return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.USER_REPETITION, "");
            }
            //调用service层方法
            int flag = userService.addUser(user);
            if (flag == 1) {
                return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.INSERT_SUCCES, "");
            } else {
                return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.INSERT_FAILD, "");
            }
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }

    /**
     * 登录
     *
     * @param user
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    @PassToken
    @ApiOperation(value = "登录", notes = "登录", httpMethod = "POST")
    public ResponseEntity<BaseResponse> login(@RequestBody User user) throws Exception {
        User userForBase = userService.getUserByName(user.getUserName());
        if (userForBase == null) {
            return BaseResponse.generateBadResponseEntity(Constant.USNO_CODE, Constant.USRE_ERROR, "");
        } else {
            if (!userForBase.getPassword().equals(user.getPassword())) {
                return BaseResponse.generateBadResponseEntity(Constant.ERROR_CODE, Constant.PWD_ERROR, "");
            } else {
                HashMap obj = new HashMap(1024);
                obj.put("token", tokenUtil.getToken(userForBase));
                obj.put("user", userForBase);
                return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.LOGIN_SUCCES, obj);
            }
        }
    }

    /**
     * 分页
     *
     * @param user
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询", notes = "获取用户列表", httpMethod = "GET")
    public ResponseEntity<BaseResponse<Paginating<User>>> getUserList(User user) {
        log.debug(user.toString());
        try {
            Paginating<User> paginating = userService.getUserList(user);
            return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.SELECT_SUCCES, paginating);
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息", httpMethod = "POST")
    public ResponseEntity<BaseResponse> updateUser(
            @RequestBody User user) {
        try {
            int flag = userService.updateUser(user);
            if (flag == 1) {
                return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.UPADTA_SUCCES, "");
            } else {
                return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
            }
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }

    /**
     * 根据id删除用户
     *
     * @param request
     * @param user
     * @return
     */
    @DeleteMapping("/del")
    @ApiOperation(value = "删除用户", notes = "根据id删除用户", httpMethod = "DELETE")
    public ResponseEntity<BaseResponse> delUser(HttpServletRequest request, @RequestBody User user) {
        try {
            //判断用户名是admin
            if (user != null && 1 == user.getUserid()) {
                return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.ADMIN_NODELETE, "");
            }
            int flag = userService.delUser(user.getUserid());
            if (flag == 1) {
                return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.USER_DELETE_SUCCESS, "");
            } else {
                return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.USER_DELETE_FAILD, "");
            }
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }
}
